/**
 * created by 小卷毛, 2019-10-09 009
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.baidu.ocr.ui.util

import android.content.Context
import android.util.Log
import com.baidu.ocr.sdk.OCR
import com.baidu.ocr.sdk.OnResultListener
import com.baidu.ocr.sdk.exception.OCRError
import com.baidu.ocr.sdk.model.AccessToken
import com.baidu.ocr.sdk.model.IDCardParams
import com.baidu.ocr.sdk.model.IDCardResult

/**
 * 项目名称：Smart_Business
 * 创建人：小卷毛
 * 创建时间：2019-10-09 16:25
 */
fun Context.initAccessToken(listener: _OnResultListener<AccessToken>.() -> Unit) {
    val mListener = _OnResultListener<AccessToken>()
    mListener.listener()
    OCR.getInstance(this)
        .initAccessToken(mListener, this)
}

fun Context.recognizeIDCard(
    param: IDCardParams,
    listener: _OnResultListener<IDCardResult>.() -> Unit
) {
    val mListener = _OnResultListener<IDCardResult>()
    mListener.listener()
    OCR.getInstance(this).recognizeIDCard(param, mListener)
}

open class _OnResultListener<T> : OnResultListener<T> {

    private var _onResult: ((T) -> Unit)? = null
    private var _onError: ((OCRError) -> Unit)? = null
    private var _onFinally: (() -> Unit)? = null

    fun onResult(listener: (T) -> Unit) {
        _onResult = listener
    }

    fun onError(listener: (OCRError) -> Unit) {
        _onError = listener
    }

    fun onFinally(listener: () -> Unit) {
        _onFinally = listener
    }

    /**
     * 调用成功，返回 T 对象
     */
    override fun onResult(result: T) {
        Log.i("OCR", "调用成功")
        _onResult?.invoke(result)
        _onFinally?.invoke()
    }

    /**
     * 调用失败，返回OCRError子类SDKError对象
     */
    override fun onError(error: OCRError) {
        Log.e("OCR", "调用失败，errorCode = ${error.errorCode}，msg = ${error.message}")
        _onError?.invoke(error)
        _onFinally?.invoke()
    }

}